package bb.lanxing.lib.devices.utils;

//import androidx.core.view.ViewCompat;
//import com.igexin.push.f.r;
//import com.umeng.analytics.pro.cc;

import bb.lanxing.lib.devices.sprint.ymodem.YModems;
import java.io.UnsupportedEncodingException;

public class ByteUtils {
	public static int byte2Int(byte b) {
		return b & 255;
	}

	public static short toShortBE(byte[] bArr, int i) {
		return (short) ((bArr[i + 1] & 255) | ((bArr[i] & 255) << 8));
	}

	public static short toShortLE(byte[] bArr, int i) {
		return (short) (((bArr[i + 1] & 255) << 8) | (bArr[i] & 255));
	}

	public static void shortToBytesBe(short s, byte[] bArr, int i) {
		bArr[i + 1] = (byte) (s & 255);
		bArr[i] = (byte) (s >> 8);
	}

	public static void intToBytesLE(int i, byte[] bArr, int i2) {
		bArr[i2] = (byte) (i & 255);
		bArr[i2 + 1] = (byte) ((i >>> 8) & 255);
		bArr[i2 + 2] = (byte) ((i >>> 16) & 255);
		bArr[i2 + 3] = (byte) ((i >>> 24) & 255);
	}

	public static int bytesToIntBE(byte[] bArr, int i) {
		return (bArr[i + 3] & 255) | ((bArr[i] << YModems.CAN) & 0xFF000000) | ((bArr[i + 1] << (byte) 16/*cc.n*/) & 0xFF0000) | ((bArr[i + 2] << 8) & 0xFF00);
	}

	public static String toHex(byte b) {
		String hexString = Integer.toHexString(b & 255);
		if (hexString.length() < 2) {
			return "0" + hexString;
		}
		return hexString;
	}

	public static byte[] stringToBytes(String str, String str2) {
		if (str != null && !str.isEmpty()) {
			try {
				return str.getBytes(str2);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public static String bytesToString(byte[] bArr, String str) {
		if (assertNull(bArr)) {
			return null;
		}
		try {
			return new String(bArr, str);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static byte[] stringToBytes(String str) {
		return stringToBytes(str, "utf-8");
	}

	public static boolean assertNull(byte[] bArr, int i, int i2) {
		return !assertNotNull(bArr, i, i2);
	}

	public static boolean assertNull(byte[] bArr) {
		return !assertNotNull(bArr, 0, bArr != null ? bArr.length : 0);
	}

	public static boolean assertNotNull(byte[] bArr, int i, int i2) {
		if (!(bArr == null || bArr.length == 0)) {
			for (int i3 = 0; i3 < i2; i3++) {
				int i4 = i + i3;
				if (i4 >= bArr.length) {
					break;
				} else if (bArr[i4] != 0) {
					return true;
				}
			}
		}
		return false;
	}

	public static boolean assertNotNull(byte[] bArr) {
		return assertNotNull(bArr, 0, bArr != null ? bArr.length : 0);
	}

	public static final String printlnByteArrayToUnsignHexString(byte[] bArr, int i, int i2) {
		StringBuffer stringBuffer = new StringBuffer();
		for (int i3 = 0; i3 < i2 && i3 + i < bArr.length; i3++) {
			String hexString = Integer.toHexString(bArr[i3] & 255);
			if (hexString.length() == 1) {
				hexString = "0" + hexString;
			}
			stringBuffer.append(hexString);
			stringBuffer.append(" ");
		}
		return stringBuffer.toString();
	}

	public static String printlnByteArrayToUnsignHexString(byte[] bArr) {
		return bArr != null ? printlnByteArrayToUnsignHexString(bArr, 0, bArr.length) : "";
	}

	public static byte hexToByte(String str) {
		return (byte) Integer.parseInt(str, 16);
	}
}
